package com.chukun.multi.activeobject;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author chukun
 *  客户端触发请求操作
 */
public class MMSDeliveryServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 将请求中的数据解析
        MMSDeliveryRequest mmsDeliveryReq = this.parseRequest(req.getInputStream());
        Recipient shortNumberRecipient = mmsDeliveryReq.getRecipient();
        Recipient originalNumberRecipient = null;

        try {
            originalNumberRecipient = this.convertShortNumber(shortNumberRecipient);
        } catch (SQLException e) {
            // 转换失败，触发消息缓存的操作
             AsyncRequestPersistence.getInstance().store(mmsDeliveryReq);
            // 省略其他的代码
            resp.setStatus(202);
        }
    }

    /**
     * 将短号码，转换为长号码
     * @param shortNumberRecipient
     * @return
     * @throws SQLException
     */
    private Recipient convertShortNumber(Recipient shortNumberRecipient) throws SQLException {

        Recipient recipient = new Recipient();
        // 省略实现的代码
        return recipient;
    }

    /**
     * 包装request请求
     * @param inputStream
     * @return
     */
    private MMSDeliveryRequest parseRequest(ServletInputStream inputStream) {

        MMSDeliveryRequest request = new MMSDeliveryRequest();
        // 省略实现的代码
        return request;
    }
}
